package com.sl.au.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.ConfigDetailForCRM;
import com.sl.au.entity.ConfigMainForCRM;
import com.sl.au.entity.EXDEgetPayBackTypeAreaPerson;
import com.sl.au.entity.MFOldPartRelation;
import com.sl.au.entity.ProductPlanForCRM;
import com.sl.au.entity.Quote;
import com.sl.au.entity.QuoteForeign;
import com.sl.au.entity.QuoteReplacement;
import com.sl.au.repository.ConfigDetailForCRMRepository;
import com.sl.au.repository.MFOldPartRelationRepository;
import com.sl.au.repository.QuoteForeignRepository;
import com.sl.au.repository.QuoteReplacementRepository;
import com.sl.au.repository.QuoteRepository;
import com.sl.au.service.ConfigDetailForCRMService;
import com.sl.au.service.QueryService;

@Service("ConfigDetailForCRMService")
@Transactional
public class ConfigDetailForCRMServiceImpl extends BaseServiceImpl implements ConfigDetailForCRMService {

	@Autowired
	ConfigDetailForCRMRepository configDetailForCRMRepository;
	@Autowired
	QuoteReplacementRepository quoteReplacementRepository;
	@Autowired
	QuoteForeignRepository quoteForeignRepository;
	@Autowired
	QuoteRepository quoteRepository;
	@Autowired
	MFOldPartRelationRepository mFOldPartRelationRepository;
	@Autowired
	QueryService queryService;
	
	@Override
	public ConfigDetailForCRMRepository getConfigDetailForCRMRepository() {
		return configDetailForCRMRepository;
	}

	@Override
	public List<ConfigDetailForCRM> getDetails(int start, int length, Map<String, String> condition, String orderName,
			String dir) {
		Pageable pageable=new PageRequest(start/length,length);
		List<ConfigDetailForCRM> list = null;
		list = configDetailForCRMRepository.getDetails(condition.get("key"), Integer.parseInt(condition.get("opId")),condition.get("ntype"), pageable);
		return list;
	}

	@Override
	public long getDetailsCount(Map<String, String> condition) {
		return configDetailForCRMRepository.getDetailsCount(condition.get("key"), Integer.parseInt(condition.get("opId")),condition.get("ntype"));
	}

	@Override
	public int saveOtherER(String[] id, String quoteId, int indexOrder) {
		int code = 0;
		int number = 0;
		Pageable pageable = new PageRequest(0 / Integer.MAX_VALUE, Integer.MAX_VALUE, Sort.Direction.ASC, "indexOrder");
		List<QuoteReplacement> quoteReplacements = quoteRepository.findQuoteReplacement(quoteId, pageable).getContent();
		int size = quoteReplacements.size();
		int idsize = id.length;
		if(indexOrder < 1){
			if(size != 0){
				number = quoteReplacements.get(size-1).getIndexOrder() + 1;
			}else{
				number = 1;
			}
		}else{
			number = indexOrder;
			for(int j = 0;j < quoteReplacements.size();j++){
				QuoteReplacement q = quoteReplacements.get(j);
				if(q.getIndexOrder() < number){
					continue;
				}
				q.setIndexOrder(q.getIndexOrder() + idsize);
				quoteReplacementRepository.save(q);
			}
		}
		Quote quote = quoteRepository.findOne(quoteId);
		for(int i=0;i<id.length;i++){
			System.out.println(id[i]);
			ConfigDetailForCRM configDetailForCRM = configDetailForCRMRepository.findOne(Integer.valueOf(id[i]));
			QuoteReplacement QuoteReplacement = new QuoteReplacement();
			QuoteReplacement.setDeviceName(configDetailForCRM.getcPartName());//名称
			QuoteReplacement.setType(configDetailForCRM.getcPartPicNumDes());//图号--规格型号
			QuoteReplacement.setMaterial(configDetailForCRM.getcMaterial()==null?"":configDetailForCRM.getcMaterial());//材料
			QuoteReplacement.setUnit("个");//单位
			QuoteReplacement.setAmount(1);//数量
			QuoteReplacement.setPrice((float)0);//报价
			QuoteReplacement.setCostPrice((float)0);//原价
			QuoteReplacement.setIndexOrder(number);
			QuoteReplacement.setQuote(quote);
			quoteReplacementRepository.save(QuoteReplacement);
			number++;
			code = 1;
		}
		return code;
	}
	@Override
	public int saveOtherERquoteFor(String[] id, String quoteForeignId) {
		int code = 0;
		for(int i=0;i<id.length;i++){
			ConfigDetailForCRM configDetailForCRM = configDetailForCRMRepository.findOne(id[i]);
			QuoteReplacement QuoteReplacement = new QuoteReplacement();
			QuoteReplacement.setDeviceName(configDetailForCRM.getcPartName());//名称
			QuoteReplacement.setType(configDetailForCRM.getcPartPicNumDes());//图号--规格型号
			QuoteReplacement.setMaterial(configDetailForCRM.getcMaterial()==null?"":configDetailForCRM.getcMaterial());//材料
			QuoteReplacement.setUnit("个");//单位
			QuoteReplacement.setAmount(1);//数量
			QuoteReplacement.setPrice((float)0);//报价
			QuoteReplacement.setCostPrice((float)0);//原价
			QuoteForeign quoteForeign = quoteForeignRepository.findOne(quoteForeignId);
			QuoteReplacement.setQuoteForeign(quoteForeign);
			quoteReplacementRepository.save(QuoteReplacement);
			code = 1;
		}
		return code;
	}

	@Override
	public List<ProductPlanForCRM> getContractDetails(int start, int length, Map<String, String> condition,
			String orderName, String dir) {
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"id");
		List<ProductPlanForCRM> list = null;
		list = configDetailForCRMRepository.getContractDetails(condition.get("key"), pageable);
		return list;
	}

	@Override
	public long getDetailsContractCount(Map<String, String> condition) {
		return configDetailForCRMRepository.getDetailsContractCount(condition.get("key"));
	}

	@Override
	public List<MFOldPartRelation> getMFDetails(int start, int length, Map<String, String> condition, String orderName,
			String dir) {
		Pageable pageable=new PageRequest(start/length,length);
		List<MFOldPartRelation> list = null;
		list = configDetailForCRMRepository.getMFDetails(condition.get("key"), pageable);
		return list;
	}

	@Override
	public long getMFDetailsCount(Map<String, String> condition) {
		return configDetailForCRMRepository.getDetailsMFCount(condition.get("key"));
	}

	@Override
	public int saveOtherERMF(String[] id, String quoteId) {
		int code = 0;
		for(int i=0;i<id.length;i++){
			MFOldPartRelation mFOldPartRelation = mFOldPartRelationRepository.findOne(id[i]);
			QuoteReplacement QuoteReplacement = new QuoteReplacement();
			QuoteReplacement.setDeviceName(mFOldPartRelation.getcPartName());//名称
			QuoteReplacement.setType(mFOldPartRelation.getcPartPicNum());//图号--规格型号
			QuoteReplacement.setMaterial(mFOldPartRelation.getcMaterial()==null?"":mFOldPartRelation.getcMaterial());//材料
			QuoteReplacement.setUnit("个");//单位
			QuoteReplacement.setAmount(1);//数量
			QuoteReplacement.setPrice((float)0);//报价
			QuoteReplacement.setCostPrice((float)0);//原价
			Quote quote = quoteRepository.findOne(quoteId);
			QuoteReplacement.setQuote(quote);
			quoteReplacementRepository.save(QuoteReplacement);
			code = 1;
		}
		return code;
	}

	@Override
	public int saveOtherEROld(String[] id, String quoteId) {
		int code = 0;
		for(int i=0;i<id.length;i++){
			QuoteReplacement quoteReplacementOld = quoteReplacementRepository.findOne(id[i]);
			QuoteReplacement QuoteReplacement = new QuoteReplacement();
			QuoteReplacement.setDeviceName(quoteReplacementOld.getDeviceName());//名称
			QuoteReplacement.setType(quoteReplacementOld.getType());//图号--规格型号
			QuoteReplacement.setMaterial(quoteReplacementOld.getMaterial()==null?"":quoteReplacementOld.getMaterial());//材料
			QuoteReplacement.setUnit(quoteReplacementOld.getUnit());//单位
			QuoteReplacement.setAmount(quoteReplacementOld.getAmount());//数量
			QuoteReplacement.setPrice(quoteReplacementOld.getPrice());//报价
			QuoteReplacement.setCostPrice(quoteReplacementOld.getCostPrice());//原价
			Quote quote = quoteRepository.findOne(quoteId);
			QuoteReplacement.setQuote(quote);
			quoteReplacementRepository.save(QuoteReplacement);
			code = 1;
		}
		return code;
	}

	@Override
	public List<ConfigMainForCRM> getconfigmainlist(String opid) {
		List<ConfigMainForCRM> list = null;
		list = configDetailForCRMRepository.getconfigmainlist(Integer.parseInt(opid));
		return list;
	}
	
	public String findPartId(String ids,int UpPartId){
		int[] PartIds = mFOldPartRelationRepository.getpagemfsecond(UpPartId);
		if(PartIds.length > 0){
			for(int i = 0; i < PartIds.length; i++){
				ids += PartIds[i] + ",";
				ids = this.findPartId(ids,PartIds[i]);
			}
			ids += UpPartId + ",";
		}else{
			ids += UpPartId + ",";
		}
		return ids;
	}
	@PersistenceContext
	private EntityManager em;
	@Override
	public List<MFOldPartRelation> getpagemfsecond(int type, String key, int opid) {
		String ids = opid + ",";
		if(type == 2){
			ids = findPartId("",opid);
		}
		ids = ids.substring(0, ids.length()-1);
		Query query = this.em.createNativeQuery("{call getMFOldPartRelation(?,?,?)}",MFOldPartRelation.class);
		query.setParameter(1, type);
		query.setParameter(2, ids);
		query.setParameter(3, key);
		
		@SuppressWarnings("unchecked")
		List<MFOldPartRelation> users = query.getResultList();
		return users;
	}


}
